﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace MultiDAQ_Analysis.Model
{
    public class ChannelData
    {
        [JsonProperty("channelIndex")]
        public int ChannelIndex { get; set; }

        [JsonProperty("thresholds")]
        public ThresholdData Thresholds { get; set; } = new ThresholdData();

        [JsonProperty("workConditions")]
        public WorkConditionData WorkConditions { get; set; } = new WorkConditionData();

        [JsonProperty("measurementResults")]
        public MeasurementResultData MeasurementResults { get; set; } = new MeasurementResultData();

        [JsonProperty("lastModified")]
        public DateTime LastModified { get; set; } = DateTime.Now;

        [JsonProperty("stepLength")]
        public double StepLength { get; set; } = 1.0;

        [JsonProperty("realtimeStatistics")]
        public RealtimeStatisticsData RealtimeStatistics { get; set; } = new RealtimeStatisticsData();


        //标准源与工作源内容
        // 在 ChannelData 类中添加属性（每个通道一个）
        [JsonProperty("presetConditions")]
        public PresetConditionData PresetConditions { get; set; } = new PresetConditionData();

    }

    // 每个通道的预设条件数据类
    public class PresetConditionData
    {
        // 标准源效率测量页面 - 每个通道的数据
        [JsonProperty("alphaSpecificActivity")]
        public double AlphaSpecificActivity { get; set; } = 12.5; // α比活度 Bq/g

        [JsonProperty("alphaMass")]
        public double AlphaMass { get; set; } = 20; // α质量 mg

        [JsonProperty("betaSpecificActivity")]
        public double BetaSpecificActivity { get; set; } = 14.4; // β比活度 Bq/g

        [JsonProperty("betaMass")]
        public double BetaMass { get; set; } = 20; // β质量 mg

        // 效率测量页面 - 每个通道的数据
        [JsonProperty("alphaSurfaceEmission")]
        public double AlphaSurfaceEmission { get; set; } = 1440; // α表面发射率

        [JsonProperty("betaSurfaceEmission")]
        public double BetaSurfaceEmission { get; set; } = 1594; // β表面发射率
    }

    public class ThresholdData
    {
        [JsonProperty("alphaLow")]
        public double AlphaLow { get; set; } = 1.5;

        [JsonProperty("alphaHigh")]
        public double AlphaHigh { get; set; } = 3;

        [JsonProperty("betaLow")]
        public double BetaLow { get; set; } = 0;

        [JsonProperty("betaHigh")]
        public double BetaHigh { get; set; } = 1.5;

        [JsonProperty("triggerLine")]
        public double TriggerLine { get; set; } = 0;

        [JsonProperty("isSelected")]
        public bool IsSelected { get; set; } = true;
    }

    public class RealtimeStatisticsData
    {
        [JsonProperty("totalAlphaPoints")]
        public long TotalAlphaPoints { get; set; } = 0;

        [JsonProperty("totalBetaPoints")]
        public long TotalBetaPoints { get; set; } = 0;

        [JsonProperty("totalDeductedPoints")]
        public long TotalDeductedPoints { get; set; } = 0;

        [JsonProperty("totalSamples")]
        public long TotalSamples { get; set; } = 0;

        [JsonProperty("statisticsStartTime")]
        public DateTime StatisticsStartTime { get; set; } = DateTime.Now;

        [JsonProperty("lastUpdateTime")]
        public DateTime LastUpdateTime { get; set; } = DateTime.Now;

        [JsonProperty("sessionStatistics")]
        public List<SessionStatistics> SessionStatistics { get; set; } = new List<SessionStatistics>();

        [JsonIgnore]
        public double AlphaRate => TotalSamples > 0 ? (double)TotalAlphaPoints / TotalSamples * 100 : 0;

        [JsonIgnore]
        public double BetaRate => TotalSamples > 0 ? (double)TotalBetaPoints / TotalSamples * 100 : 0;

        [JsonIgnore]
        public double DeductedRate => TotalSamples > 0 ? (double)TotalDeductedPoints / TotalSamples * 100 : 0;

        [JsonIgnore]
        public double AlphaCountsPerSecond
        {
            get
            {
                var elapsed = (DateTime.Now - StatisticsStartTime).TotalSeconds;
                return elapsed > 0 ? TotalAlphaPoints / elapsed : 0;
            }
        }

        [JsonIgnore]
        public double BetaCountsPerSecond
        {
            get
            {
                var elapsed = (DateTime.Now - StatisticsStartTime).TotalSeconds;
                return elapsed > 0 ? TotalBetaPoints / elapsed : 0;
            }
        }

        public void ResetStatistics()
        {
            TotalAlphaPoints = 0;
            TotalBetaPoints = 0;
            TotalDeductedPoints = 0;
            TotalSamples = 0;
            StatisticsStartTime = DateTime.Now;
            LastUpdateTime = DateTime.Now;
        }

        public void UpdateStatistics(long alphaCount, long betaCount, long deductedCount, long sampleCount)
        {
            TotalAlphaPoints += alphaCount;
            TotalBetaPoints += betaCount;
            TotalDeductedPoints += deductedCount;
            TotalSamples += sampleCount;
            LastUpdateTime = DateTime.Now;
        }
    }

    public class SessionStatistics
    {
        [JsonProperty("sessionId")]
        public string SessionId { get; set; } = Guid.NewGuid().ToString();

        [JsonProperty("startTime")]
        public DateTime StartTime { get; set; }

        [JsonProperty("endTime")]
        public DateTime EndTime { get; set; }

        [JsonProperty("alphaPoints")]
        public long AlphaPoints { get; set; }

        [JsonProperty("betaPoints")]
        public long BetaPoints { get; set; }

        [JsonProperty("deductedPoints")]
        public long DeductedPoints { get; set; }

        [JsonProperty("totalSamples")]
        public long TotalSamples { get; set; }

        [JsonIgnore]
        public TimeSpan Duration => EndTime - StartTime;
    }

    public class WorkConditionData
    {
        [JsonProperty("alphaBackground")]
        public double AlphaBackground { get; set; } = 0;

        [JsonProperty("betaBackground")]
        public double BetaBackground { get; set; } = 0;

        [JsonProperty("alphaWorkEfficiency")]
        public double AlphaWorkEfficiency { get; set; } = 0;

        [JsonProperty("betaWorkEfficiency")]
        public double BetaWorkEfficiency { get; set; } = 0;

        [JsonProperty("alphaStandardEfficiency")]
        public double AlphaStandardEfficiency { get; set; } = 0;

        [JsonProperty("betaStandardEfficiency")]
        public double BetaStandardEfficiency { get; set; } = 0;

        [JsonProperty("correctionFactor")]
        public double CorrectionFactor { get; set; } = 1.02;

        [JsonProperty("measurementCycle")]
        public int MeasurementCycle { get; set; } = 1;

        [JsonProperty("measurementTime")]
        public double MeasurementTime { get; set; } = 1;
    }

    public class MeasurementResultData
    {
        [JsonProperty("backgroundMeasurement")]
        public BackgroundMeasurementResult BackgroundMeasurement { get; set; }

        [JsonProperty("workingSourceMeasurement")]
        public WorkingSourceMeasurementResult WorkingSourceMeasurement { get; set; }

        [JsonProperty("standardSourceMeasurement")]
        public StandardSourceMeasurementResult StandardSourceMeasurement { get; set; }

        [JsonProperty("generalSampleResult")]
        public GeneralSampleMeasurementResult GeneralSampleResult { get; set; }

        [JsonProperty("waterSampleResult")]
        public WaterSampleMeasurementResult WaterSampleResult { get; set; }

        [JsonProperty("bioSampleResult")]
        public BioSampleMeasurementResult BioSampleResult { get; set; }

        [JsonProperty("gasSampleResult")]
        public GasSampleMeasurementResult GasSampleResult { get; set; }

        [JsonProperty("envSampleResult")]
        public EnvSampleMeasurementResult EnvSampleResult { get; set; }
    }





    // 样品基本信息
    public class SampleBasicInfo
    {
        [JsonProperty("name")]
        public string Name { get; set; } = "";

        [JsonProperty("number")]
        public string Number { get; set; } = "";

        [JsonProperty("sampler")]
        public string Sampler { get; set; } = "";

        [JsonProperty("sampleLocation")]
        public string SampleLocation { get; set; } = "";

        [JsonProperty("measurer")]
        public string Measurer { get; set; } = "";
    }

    // 本底测量结果
    public class BackgroundMeasurementResult
    {
        [JsonProperty("alphaBackground")]
        public double AlphaBackground { get; set; }

        [JsonProperty("betaBackground")]
        public double BetaBackground { get; set; }

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("measurementDuration")]
        public double MeasurementDuration { get; set; }

        [JsonProperty("alphaBackgroundUncertainty")]
        public double AlphaBackgroundUncertainty { get; set; }

        [JsonProperty("betaBackgroundUncertainty")]
        public double BetaBackgroundUncertainty { get; set; }

        [JsonProperty("isValid")]
        public bool IsValid { get; set; }
    }

    // 工作源测量结果
    public class WorkingSourceMeasurementResult
    {
        [JsonProperty("alphaEfficiency")]
        public double AlphaEfficiency { get; set; } // 工作源α效率 %

        [JsonProperty("betaEfficiency")]
        public double BetaEfficiency { get; set; } // 工作源β效率 %

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("measurementDuration")]
        public double MeasurementDuration { get; set; }

        [JsonProperty("geometryFactor")]
        public double GeometryFactor { get; set; } = 0.9; // 几何因子

        [JsonProperty("alphaEmissionRate")]
        public double AlphaEmissionRate { get; set; } = 1440; // α 2π表面发射率 min⁻¹

        [JsonProperty("betaEmissionRate")]
        public double BetaEmissionRate { get; set; } = 1594; // β 2π表面发射率 min⁻¹

        [JsonProperty("efficiencyRatio")]
        public double EfficiencyRatio { get; set; } // 效率比

        [JsonProperty("crossTalkRatio")]
        public double CrossTalkRatio { get; set; } // 串道比
    }

    // 标准源测量结果
    public class StandardSourceMeasurementResult
    {
        [JsonProperty("alphaEfficiency")]
        public double AlphaEfficiency { get; set; } // 标准源α效率

        [JsonProperty("betaEfficiency")]
        public double BetaEfficiency { get; set; } // 标准源β效率

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("measurementDuration")]
        public double MeasurementDuration { get; set; }

        [JsonProperty("sampleDiskArea")]
        public double SampleDiskArea { get; set; } = 1400; // 样品盘面积 mm²

        [JsonProperty("alphaSpecificActivity")]
        public double AlphaSpecificActivity { get; set; } = 12.5; // α标准源比活度 Bq/g

        [JsonProperty("betaSpecificActivity")]
        public double BetaSpecificActivity { get; set; } = 14.4; // β标准源比活度 Bq/g

        [JsonProperty("sourceMass")]
        public double SourceMass { get; set; } = 20; // 标准源质量 mg
    }

    // 一般样品测量结果
    public class GeneralSampleMeasurementResult
    {
        [JsonProperty("alphaActivity")]
        public double AlphaActivity { get; set; } // α活性 Bq

        [JsonProperty("betaActivity")]
        public double BetaActivity { get; set; } // β活性 Bq

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("efficiencyType")]
        public string EfficiencyType { get; set; } // "WorkSource" 或 "StandardSource"

        [JsonProperty("correctionFactor")]
        public double CorrectionFactor { get; set; } = 1.0;

        [JsonProperty("sampleInfo")]
        public SampleBasicInfo SampleInfo { get; set; } = new SampleBasicInfo();
    }

    // 水样品测量结果
    public class WaterSampleMeasurementResult
    {
        [JsonProperty("alphaConcentration")]
        public double AlphaConcentration { get; set; } // α活度浓度 Bq/L

        [JsonProperty("betaConcentration")]
        public double BetaConcentration { get; set; } // β活度浓度 Bq/L

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("sampleVolume")]
        public double SampleVolume { get; set; } // 水样体积 L (1-99L)

        [JsonProperty("totalResidueAmount")]
        public double TotalResidueAmount { get; set; } // 总残渣量 mg (1-9999mg)

        [JsonProperty("testedSampleMass")]
        public double TestedSampleMass { get; set; } // 被测样品量 mg (1-5000mg)

        [JsonProperty("efficiencyType")]
        public string EfficiencyType { get; set; } // "WorkSource" 或 "StandardSource"

        [JsonProperty("sampleInfo")]
        public SampleBasicInfo SampleInfo { get; set; } = new SampleBasicInfo();
    }

    // 生物样品测量结果
    public class BioSampleMeasurementResult
    {
        [JsonProperty("alphaConcentration")]
        public double AlphaConcentration { get; set; }

        [JsonProperty("betaConcentration")]
        public double BetaConcentration { get; set; }

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("freshSampleWeight")]
        public double FreshSampleWeight { get; set; } // 鲜样重量 kg (1-49kg)

        [JsonProperty("testedSampleMass")]
        public double TestedSampleMass { get; set; } // 被测样品量 mg (1-1000000mg)

        [JsonProperty("totalAshWeight")]
        public double TotalAshWeight { get; set; } // 总灰度 mg (1-5000mg)

        [JsonProperty("recoveryRate")]
        public double RecoveryRate { get; set; } = 1.0; // 回收率 ≤1

        [JsonProperty("efficiencyType")]
        public string EfficiencyType { get; set; } // "WorkSource" 或 "StandardSource"

        // 添加缺失的属性
        [JsonProperty("sampleMass")]
        public double SampleMass { get; set; } // 用于兼容计算器中的 SampleMass 属性

        [JsonProperty("sampleInfo")]
        public SampleBasicInfo SampleInfo { get; set; } = new SampleBasicInfo();
    }

    // 气体样品测量结果
    // 气体样品测量结果
    public class GasSampleMeasurementResult
    {
        [JsonProperty("alphaConcentration")]
        public double AlphaConcentration { get; set; }

        [JsonProperty("betaConcentration")]
        public double BetaConcentration { get; set; }

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("samplingVolume")]
        public double SamplingVolume { get; set; } // 采样体积 L (1-1000000L)

        [JsonProperty("filterEfficiency")]
        public double FilterEfficiency { get; set; } // 过滤效率 (1-100%)

        [JsonProperty("absorptionFactor")]
        public double AbsorptionFactor { get; set; } // 吸收系数 (1-100%)

        [JsonProperty("efficiencyType")]
        public string EfficiencyType { get; set; } // "WorkSource" 或 "StandardSource"

        [JsonProperty("alphaCountRate")]
        public double AlphaCountRate { get; set; } // α计数率 counts/s

        [JsonProperty("betaCountRate")]
        public double BetaCountRate { get; set; } // β计数率 counts/s

        [JsonProperty("measurementCycles")]
        public int MeasurementCycles { get; set; } // 测量次数

        [JsonProperty("measurementDuration")]
        public double MeasurementDuration { get; set; } // 测量时长 min

        // 添加缺失的属性
        [JsonProperty("sampleVolume")]
        public double SampleVolume { get; set; } // 用于兼容计算器中的 SampleVolume 属性

        [JsonProperty("sampleInfo")]
        public SampleBasicInfo SampleInfo { get; set; } = new SampleBasicInfo();
    }

    // 环境样品测量结果
    public class EnvSampleMeasurementResult
    {
        [JsonProperty("alphaConcentration")]
        public double AlphaConcentration { get; set; } // α活度 Bq/kg

        [JsonProperty("betaConcentration")]
        public double BetaConcentration { get; set; } // β活度 Bq/kg

        [JsonProperty("measurementTime")]
        public DateTime MeasurementTime { get; set; }

        [JsonProperty("testedSampleMass")]
        public double TestedSampleMass { get; set; } // 样品源质量 mg (1-5000mg)

        [JsonProperty("correctionFactor")]
        public double CorrectionFactor { get; set; } = 1.0; // 校正系数 K

        [JsonProperty("efficiencyType")]
        public string EfficiencyType { get; set; } // "WorkSource" 或 "StandardSource"

        [JsonProperty("sampleInfo")]
        public SampleBasicInfo SampleInfo { get; set; } = new SampleBasicInfo();
    }





}